<?php

namespace app\common\model;

use app\stores\model\order\GoodsOrderModel;
use think\facade\Db;


class OrderSettleModel extends CommonModel{

    protected  $name = 'beautiful_settle_order';

    protected  $pk   = 'id';

    public static function up($data,$where){
        $res=self::update($data,$where);
        return $res;
    }
    public function getList($where,$money_where,$order,$page=0,$limit=0){
        $query = $this->alias('ml')->join('shop s','ml.shop_id=s.shop_id')
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['ml.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['type']),function ($query) use ($where){
                $query->where('ml.type','=',$where['type']);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where('ml.status','=',$where['status']);
            })
            ->when(isset($where['money_status']),function ($query) use ($where){
                $query->where(['ml.money_status'=>$where['money_status']]);
            })
            ->when(!empty($where['add_start_time']),function ($query) use ($where){
                $query->where('ml.add_time','>=',$where['add_start_time']);
            })
            ->when(!empty($where['add_end_time']),function ($query) use ($where){
                $query->where('ml.add_time','<=',$where['add_end_time']);
            })->field("ml.type,ml.status,ml.money_status,ml.g_order_id,ml.money,ml.add_time,s.name as shop_name");
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();

        $out_money = $this->where($money_where)->where(['status'=>0])->sum('money');
        $out_money = sprintf("%.2f", $out_money);
        $income_money = $this->where($money_where)->where(['status'=>1])->sum('money');
        $income_money = sprintf("%.2f", $income_money);

        if(!empty($list)){
            foreach ($list as $k=>$v){
                if($v['type']==4){
                    $goods_order = GoodsOrderModel::user_order(['order_id'=>$v['g_order_id']]);
                }
                $list[$k]['tg_member_name'] = isset($goods_order)?$goods_order['nick_name']:null;
            }
        }

        return compact('count','list','out_money','income_money');
    }
    public static function list($where,$order=['a.id'=>'desc'],$page=1,$limit=10){
        $query = Db::name('member_money_detail')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('beautiful_member c','a.member_id=c.member_id')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field("a.member_id,a.shop_id,b.name as shop_name,c.nick_name,a.add_time");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return compact('count','list');
    }
    public static function plist($where,$order=['a.id'=>'desc'],$page=1,$limit=10){
        $query = Db::name('member_money_detail')->alias('a')
            ->leftJoin('beautiful_order b','a.g_order_id=b.order_id and a.g_order_id>0')
            ->leftJoin('beautiful_member c','b.member_id=c.member_id and a.g_order_id>0')
            ->leftJoin('beautiful_buy_order d','a.b_order_id=d.buy_order_id and a.b_order_id>0')
            ->leftJoin('beautiful_member e','d.member_id=e.member_id and a.b_order_id>0')
            ->leftJoin('beautiful_partner_bonus f','a.partner_bonus_id=f.partner_bonus_id and a.partner_bonus_id>0')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(isset($where['partner_id']),function ($query) use ($where){
                $query->where(['a.partner_id'=>$where['partner_id']]);
            })
            ->when(isset($where['types']),function ($query) use ($where){
                $query->where('a.type','In',$where['types']);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field("a.id,a.add_time,a.type,c.nick_name as g_nick_name,e.nick_name as b_nick_name,a.money,f.start_time,f.end_time");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return compact('count','list');
    }
}

